home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / utilities / cli / kill277.lha / Kill.c next >
Encoding:
C/C++ Source or Header  |  1995-01-24  |  8.9 KB  |  358 lines

  1. /*
  2. ** $VER: Kill.c 2.105 (24.1.95)
  3. **
  4. ** Beinahe dasselbe wie C:Delete, nur daß man zusätzlich wie bei
  5. ** List SINCE und UPTO angeben kann
  6. ** (und das Programm informativer ist (bei löschgeschützten Files))
  7. **
  8. ** (W) 1994/95 by Walter Dörwald
  9. */
  10.  
  11. BEGFOLD // Includes
  12. #include <exec/memory.h>
  13. #include <dos/datetime.h>
  14. #include <clib/macros.h>
  15. #include <proto/exec.h>
  16. #include <proto/dos.h>
  17. #include <proto/intuition.h>
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21.  
  22. #include <privat/Defs.h>
  23. #include <privat/Auto.h>
  24. #include <privat/Proto.h>
  25.  
  26. #include <debug/debug.h>
  27.  
  28. // !!! Wo ist denn dieses FORMAT_DEF definiert? (in dos.library/StrToDate() ist's jedenfalls dokumentiert)
  29. #define FORMAT_DEF 4
  30.  
  31. #include "rev.h"
  32. ENDFOLD
  33.  
  34. BEGFOLD // Defines
  35. #define PROGRAM "Kill"
  36.  
  37. #define ARG_TEMPLATE "FILE/M/A,ALL/S,QUIET/S,FORCE/S,SINCE/K,UPTO/K,SINCEDAYS/K/N,UPTODAYS/K/N,BIGGER/K/N,SMALLER/K/N,PLAIN/S,ANSI/S"
  38. #define ARG_FILE         0
  39. #define ARG_ALL          1
  40. #define ARG_QUIET        2
  41. #define ARG_FORCE        3
  42. #define ARG_SINCE        4
  43. #define ARG_UPTO         5
  44. #define ARG_SINCEDAYS    6
  45. #define ARG_UPTODAYS     7
  46. #define ARG_BIGGER       8
  47. #define ARG_SMALLER      9
  48. #define ARG_PLAIN       10
  49. #define ARG_ANSI        11
  50. #define ARG_sizeof      12
  51.  
  52. #define BOLD_STR CSI_STR "1m"
  53. #define NORM_STR CSI_STR "0m"
  54. ENDFOLD
  55.  
  56. BEGFOLD // Variablen
  57. static const char Version[] = "$VER: " PROGRAM " " VERSION_STR "." REVISION_STR " (" HEUTE_STR ")";
  58.  
  59. struct RDArgs *ArgsPtr;
  60. UBYTE *ArgArray[ARG_sizeof];
  61.  
  62. struct DateStamp Jetzt;
  63. struct DateStamp Since;
  64. struct DateStamp Upto;
  65. struct DateStamp SinceDays;
  66. struct DateStamp UptoDays;
  67.  
  68. BOOL UseANSIFlag;
  69. ENDFOLD
  70.  
  71. BEGFOLD BOOL INLINE IsDir(struct AnchorPath *AP)
  72. {
  73.     if ((AP->ap_Info.fib_DirEntryType >= 0) && (AP->ap_Info.fib_DirEntryType != ST_SOFTLINK))
  74.         return (TRUE);
  75.     else
  76.         return (FALSE);
  77. }
  78. ENDFOLD
  79.  
  80. BEGFOLD BOOL INLINE UseANSI(void)
  81. {
  82.     if (ArgArray[ARG_PLAIN])
  83.         return (FALSE);
  84.     else if (ArgArray[ARG_ANSI])
  85.         return (TRUE);
  86.     else
  87.         return ((BOOL)IsInteractive(Output()));
  88. }
  89. ENDFOLD
  90.  
  91. BEGFOLD void DoExit(ULONG result)
  92. {
  93.     if (ArgsPtr)
  94.         FreeArgs(ArgsPtr);
  95.  
  96.     exit(result);
  97. ENDFOLD
  98.  
  99. BEGFOLD void PrintError(LONG Error,STRPTR Text)
  100. {
  101.     D2(bug("Error=%ld, Text=»%s«\n",Error,Text));
  102.  
  103.     if (!ArgArray[ARG_QUIET])
  104.     {
  105.         if (!PrintFault(Error,Text))
  106.             Printf("%s: Error %ld",Text,Error);
  107.     }
  108. ENDFOLD
  109.  
  110. BEGFOLD LONG Kill(struct AnchorPath *AP)
  111. {
  112.     LONG Error;
  113.  
  114.     D2(bug("AP.ap_Buf=»%s«\n",AP->ap_Buf));
  115.     if (!ArgArray[ARG_QUIET])
  116.         Printf("%s",AP->ap_Buf);
  117.  
  118.     if (AP->ap_Info.fib_Protection & FIBF_DELETE) // Beim löschgeschützten Files vorher entriegeln
  119.     {
  120.         if (ArgArray[ARG_FORCE]) // ... aber nur wenn es der Benutzer auch will
  121.         {
  122.             if (SetProtection(AP->ap_Info.fib_FileName,AP->ap_Info.fib_Protection & ~FIBF_DELETE))
  123.             {
  124.                 if (!ArgArray[ARG_QUIET])
  125.                 {
  126.                     if (UseANSIFlag)
  127.                         Printf(" " BOLD_STR "unprotected" NORM_STR " and");
  128.                     else
  129.                         Printf(" unprotected and");
  130.                 }
  131.             }
  132.             else
  133.             {
  134.                 D(bug("SetProtection() fehlgeschlagen\n"));
  135.                 if (UseANSIFlag)
  136.                     PrintError(IoErr()," " BOLD_STR "not" NORM_STR " unprotected");
  137.                 else
  138.                     PrintError(IoErr()," not unprotected");
  139.                 return (RETURN_ERROR);
  140.             }
  141.         }
  142.         else
  143.         {
  144.             SetIoErr(ERROR_DELETE_PROTECTED);
  145.             if (UseANSIFlag)
  146.                 PrintError(ERROR_DELETE_PROTECTED," " BOLD_STR "not" NORM_STR " deleted (use FORCE/S)");
  147.             else
  148.                 PrintError(ERROR_DELETE_PROTECTED," not deleted (use FORCE/S)");
  149.             return (RETURN_WARN);
  150.         }
  151.     }
  152.     if (DeleteFile(AP->ap_Info.fib_FileName))
  153.     {
  154.         if (!ArgArray[ARG_QUIET])
  155.             Printf(" deleted\n");
  156.         return (RETURN_OK);
  157.     }
  158.     else
  159.     {
  160.         D(bug("DeleteFile() fehlgeschlagen\n"));
  161.         Error = IoErr();
  162.         if (UseANSIFlag)
  163.             PrintError(Error," " BOLD_STR "not" NORM_STR " deleted");
  164.         else
  165.             PrintError(Error," not deleted");
  166.         return ((Error == ERROR_DIRECTORY_NOT_EMPTY) ? RETURN_WARN : RETURN_ERROR);
  167.     }
  168. }
  169. ENDFOLD
  170.  
  171. BEGFOLD LONG Do(struct AnchorPath *AP)
  172. {
  173.     if (ArgArray[ARG_SINCE] && (CompareDates(&Since,&AP->ap_Info.fib_Date)<0))
  174.     {
  175.         D2(bug("AP.ap_Buf=»%s« to old (SINCE/K) (DateStamp=={%ld,%ld,%ld})\n",AP->ap_Buf,AP->ap_Info.fib_Date.ds_Days,AP->ap_Info.fib_Date.ds_Minute,AP->ap_Info.fib_Date.ds_Tick));
  176.         return (RETURN_OK);
  177.     }
  178.     if (ArgArray[ARG_UPTO] && (CompareDates(&Upto,&AP->ap_Info.fib_Date)>0))
  179.     {
  180.         D2(bug("AP.ap_Buf=»%s« to new (UPTO/K) (DateStamp=={%ld,%ld,%ld})\n",AP->ap_Buf,AP->ap_Info.fib_Date.ds_Days,AP->ap_Info.fib_Date.ds_Minute,AP->ap_Info.fib_Date.ds_Tick));
  181.         return (RETURN_OK);
  182.     }
  183.     if (ArgArray[ARG_SINCEDAYS] && (CompareDates(&SinceDays,&AP->ap_Info.fib_Date)<0))
  184.     {
  185.         D2(bug("AP.ap_Buf=»%s« to old (SINCEDAYS/K) (DateStamp=={%ld,%ld,%ld})\n",AP->ap_Buf,AP->ap_Info.fib_Date.ds_Days,AP->ap_Info.fib_Date.ds_Minute,AP->ap_Info.fib_Date.ds_Tick));
  186.         return (RETURN_OK);
  187.     }
  188.     if (ArgArray[ARG_UPTODAYS] && (CompareDates(&UptoDays,&AP->ap_Info.fib_Date)>0))
  189.     {
  190.         D2(bug("AP.ap_Buf=»%s« to new (UPTODAYS/K) (DateStamp=={%ld,%ld,%ld})\n",AP->ap_Buf,AP->ap_Info.fib_Date.ds_Days,AP->ap_Info.fib_Date.ds_Minute,AP->ap_Info.fib_Date.ds_Tick));
  191.         return (RETURN_OK);
  192.     }
  193.     if (ArgArray[ARG_BIGGER] && !IsDir(AP) && (AP->ap_Info.fib_Size<=*(LONG *)ArgArray[ARG_BIGGER]))
  194.     {
  195.         D2(bug("AP.ap_Buf=»%s« to small (BIGGER) (Size==%ld)\n",AP->ap_Buf,AP->ap_Info.fib_Size));
  196.         return (RETURN_OK);
  197.     }
  198.     if (ArgArray[ARG_SMALLER] && !IsDir(AP) && (AP->ap_Info.fib_Size>=*(LONG *)ArgArray[ARG_SMALLER]))
  199.     {
  200.         D2(bug("AP.ap_Buf=»%s« to big (SMALLER) (Size==%ld)\n",AP->ap_Buf,AP->ap_Info.fib_Size));
  201.         return (RETURN_OK);
  202.     }
  203.  
  204.     return (Kill(AP));
  205. ENDFOLD
  206.  
  207. BEGFOLD // Flags für Scan
  208. // Alle Unterverzeichnisse durchlaufen
  209. #define SB_ALL 0
  210. #define SF_ALL (1L<<SB_ALL)
  211.  
  212. // Alle Ausgaben unterdrücken
  213. #define SB_QUIET 1
  214. #define SF_QUIET (1L<<SB_QUIET)
  215. ENDFOLD
  216.  
  217. BEGFOLD LONG Scan(STRPTR *Files,LONGBITS BreakBits,LONGBITS Flags,LONG NameLen)
  218. {
  219.     struct AnchorPath *AP;
  220.     LONG               Error;
  221.     LONG               Level;
  222.     LONG               TestLevel;
  223.  
  224.     Level = RETURN_OK;
  225.  
  226.     if (AP = AllocVec(sizeof(struct AnchorPath)+NameLen,MEMF_ANY|MEMF_CLEAR))
  227.     {
  228.         AP->ap_BreakBits = BreakBits;
  229.         AP->ap_FoundBreak = 0;
  230.         AP->ap_Flags = APF_DOWILD;
  231.         AP->ap_Strlen = NameLen;
  232.  
  233.         for (;*Files;Files++)
  234.         {
  235.             for (Error = MatchFirst(*Files,AP);!Error;Error = MatchNext(AP))
  236.             {
  237.                 BPTR OldLock;
  238.  
  239.                 D2(bug("File »%s«, DateStamp {%ld,...}\n",AP->ap_Buf,AP->ap_Info.fib_Date.ds_Days));
  240.  
  241.                 if (AP->ap_Flags & APF_DIDDIR) // Directory fertig?
  242.                     AP->ap_Flags &= ~APF_DIDDIR;
  243.                 else if (IsDir(AP)) // Ein Directory?
  244.                 {
  245.                     if (Flags & SF_ALL) // Rein in's Directory aber noch nicht löschen sondern erst bei Rückkehr
  246.                     {
  247.                         AP->ap_Flags |= APF_DODIR;
  248.                         continue;
  249.                     }
  250.                 }
  251.  
  252.                 OldLock = CurrentDir(AP->ap_Current->an_Lock);
  253.  
  254.                 TestLevel = Do(AP);
  255.                 Level = MAX(Level,TestLevel);
  256.  
  257.                 CurrentDir(OldLock);
  258.                 if (Level>RETURN_WARN)
  259.                     goto fertig;
  260.             }
  261.             MatchEnd(AP);
  262.             // Den Fehlerlevel zurückgeben
  263.             if (Error!=ERROR_NO_MORE_ENTRIES)
  264.                 Level = RETURN_ERROR;
  265.         }
  266.     }
  267.     else
  268.     {
  269.         D(bug("AllocVec() fehlgeschlagen: Kein Speicher für struct AnchorPath\n"));
  270.         return (RETURN_FAIL);
  271.     }
  272.     fertig:
  273.     FreeVec(AP);
  274.     return (Level);
  275. ENDFOLD
  276.  
  277. BEGFOLD void main()
  278. {
  279.     LONG Level;
  280.     LONG Flags;
  281.     struct DateTime Zeit;
  282.  
  283.     DateStamp(&Jetzt);
  284.  
  285.     CLEARVAR(Zeit);
  286.     Zeit.dat_Format = FORMAT_DEF;
  287.     //Zeit.dat_Flags = 0; // DTF_SUBST braucht laut dos.library/StrToDate() nicht gesetzt zu sein
  288.  
  289.     if (!(ArgsPtr = ReadArgs(ARG_TEMPLATE,(LONG *)&ArgArray,NULL)))
  290.     {
  291.         D(bug("ReadArgs() fehlgeschlagen\n"));
  292.         PrintFault(IoErr(),PROGRAM "(parsing arguments)");
  293.         DoExit(RETURN_ERROR);
  294.     }
  295.  
  296.     Flags = 0L;
  297.     if (ArgArray[ARG_ALL])
  298.         Flags |= SF_ALL;
  299.     if (ArgArray[ARG_QUIET])
  300.         Flags |= SF_QUIET;
  301.     if (ArgArray[ARG_SINCE])
  302.     {
  303.         Zeit.dat_StrDate = ArgArray[ARG_SINCE];
  304.         if (!StrToDate(&Zeit))
  305.         {
  306.             D(bug("StrToDate() fehlgeschlagen (bei ARG_SINCE)\n"));
  307.             PrintFault(ERROR_OBJECT_WRONG_TYPE,PROGRAM "(parsing SINCE/K)");
  308.             DoExit(RETURN_ERROR);
  309.         }
  310.         Zeit.dat_Stamp.ds_Minute = 0;
  311.         Zeit.dat_Stamp.ds_Tick = 0;
  312.         Since = Zeit.dat_Stamp;
  313.  
  314.         D2(bug("Since=={%ld,%ld,%ld}\n",Since.ds_Days,Since.ds_Minute,Since.ds_Tick));
  315.     }
  316.     if (ArgArray[ARG_UPTO])
  317.     {
  318.         Zeit.dat_StrDate = ArgArray[ARG_UPTO];
  319.         if (!StrToDate(&Zeit))
  320.         {
  321.             D(bug("StrToDate() fehlgeschlagen (bei ARG_UPTO)\n"));
  322.             PrintFault(ERROR_OBJECT_WRONG_TYPE,PROGRAM "(parsing UPTO/K)");
  323.             DoExit(RETURN_ERROR);
  324.         }
  325.         Zeit.dat_Stamp.ds_Minute = 24*60-1;
  326.         Zeit.dat_Stamp.ds_Tick = TICKS_PER_SECOND-1;
  327.         Upto = Zeit.dat_Stamp;
  328.  
  329.         D2(bug("Upto=={%ld,%ld,%ld}\n",Upto.ds_Days,Upto.ds_Minute,Upto.ds_Tick));
  330.     }
  331.     if (ArgArray[ARG_SINCEDAYS])
  332.     {
  333.         SinceDays = Jetzt;
  334.         SinceDays.ds_Days += *(LONG *)ArgArray[ARG_SINCEDAYS];
  335.  
  336.         D2(bug("SinceDays=={%ld,%ld,%ld}\n",SinceDays.ds_Days,SinceDays.ds_Minute,SinceDays.ds_Tick));
  337.  
  338.     }
  339.     if (ArgArray[ARG_UPTODAYS])
  340.     {
  341.         UptoDays = Jetzt;
  342.         UptoDays.ds_Days += *(LONG *)ArgArray[ARG_UPTODAYS];
  343.  
  344.         D2(bug("UptoDays=={%ld,%ld,%ld}\n",UptoDays.ds_Days,UptoDays.ds_Minute,UptoDays.ds_Tick));
  345.     }
  346.     UseANSIFlag = UseANSI();
  347.  
  348.     if ((Level = Scan((STRPTR *)ArgArray[ARG_FILE],SIGBREAKF_CTRL_C,Flags,256L))>RETURN_WARN)
  349.         PrintFault(IoErr(),PROGRAM);
  350.  
  351.     DoExit(Level);
  352. ENDFOLD
  353.